//
//  LYStackLayout.m
//  SelectCity
//
//  Created by liuyan on 2020/1/13.
//  Copyright © 2020 liuyan. All rights reserved.
//

#import "LYStackLayout.h"

@interface LYStackLayout()

@property (nonatomic, strong) NSDictionary *layoutAttributes;

@end

@implementation LYStackLayout

- (instancetype)init{
    self = [super init];
    if (self) [self initLayout];
    return self;
}

- (void)initLayout {
    //默认卡片距离顶部10
    self.layoutMargin = UIEdgeInsetsMake(10.0, 0.0, 0.0, 0.0);
    //俩卡片不重叠的高度
    self.topReveal = 120.0;
    self.headerHeight = 0;
}

#pragma mark - Accessors

- (void)setTopReveal:(CGFloat)topReveal {
    if (topReveal != self.topReveal) {
        _topReveal = topReveal;
        [self invalidateLayout];
    }
}

- (void)setLayoutMargin:(UIEdgeInsets)margins {
    if (!UIEdgeInsetsEqualToEdgeInsets(margins, self.layoutMargin)) {
        _layoutMargin = margins;
        [self invalidateLayout];
    }
}

- (void)setItemSize:(CGSize)itemCellSize {
    if (!CGSizeEqualToSize(itemCellSize, self.itemCellSize)) {
        _itemCellSize = itemCellSize;
        [self invalidateLayout];
    }
}

- (void)setHeaderHeight:(CGFloat)headerHeight{
    if (headerHeight != self.headerHeight) {
        _headerHeight = headerHeight;
        [self invalidateLayout];
    }
}

#pragma mark - Layout computation
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

- (CGSize)collectionViewContentSize {
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    CGFloat height = self.topReveal*count + self.layoutMargin.top - self.collectionView.contentInset.bottom + self.headerHeight + 15;
    CGSize contentSize = CGSizeMake(self.collectionView.size.width,height);
    return contentSize;
}

- (void)prepareLayout {
    [self collectionViewContentSize];
    
    CGFloat itemReveal = self.topReveal;
    CGSize itemSize = self.itemCellSize;
    if (CGSizeEqualToSize(itemSize, CGSizeZero)) {
        itemSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds) - self.layoutMargin.left - self.layoutMargin.right, CGRectGetHeight(self.collectionView.bounds) - self.layoutMargin.top - self.layoutMargin.bottom - self.collectionView.contentInset.top - self.collectionView.contentInset.bottom);
    }
    CGPoint contentOffset = self.collectionView.contentOffset;
    NSMutableDictionary *layoutAttributes = [NSMutableDictionary dictionary];
    NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
    NSInteger overlappingCount = 0;
    static NSInteger firstCompressingItem = -1;
    for (NSInteger item = 0; item < itemCount; item++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attributes.zIndex = item;
        attributes.frame = CGRectMake(self.layoutMargin.left, self.headerHeight + self.layoutMargin.top + itemReveal * item, itemSize.width, itemSize.height);

        if (contentOffset.y + self.collectionView.contentInset.top < 0.0) {
            CGRect frame = attributes.frame;
            attributes.frame = frame;
        } else if (CGRectGetMinY(attributes.frame) < contentOffset.y + self.layoutMargin.top) {
            CGRect frame = attributes.frame;
            frame.origin.y = contentOffset.y + self.layoutMargin.top;
            attributes.frame = frame;
            ++overlappingCount;
        } else if (self.collectionViewContentSize.height > CGRectGetHeight(self.collectionView.bounds) && contentOffset.y > self.collectionViewContentSize.height - CGRectGetHeight(self.collectionView.bounds)) {
            if (firstCompressingItem < 0) {
                firstCompressingItem = item;
            } else {
                CGRect frame = attributes.frame;
                frame.origin.y = MAX(frame.origin.y, contentOffset.y + self.layoutMargin.top);
                attributes.frame = frame;
            }
        } else {
            firstCompressingItem = -1;
        }
        layoutAttributes[indexPath] = attributes;
    }
    self.layoutAttributes = layoutAttributes;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *layoutAttributes = [NSMutableArray array];
    [self.layoutAttributes enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *stop) {
        if (CGRectIntersectsRect(rect, attributes.frame)) {
            [layoutAttributes addObject:attributes];
        }
    }];
    return layoutAttributes;
}



@end
